/*
 * @(#) $Header$
 *
 * Copyright (C) 2008  Forklabs Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.media.jai;

import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.RenderedImage;
import javax.media.jai.ROI;
import javax.media.jai.ROIShape;

/**
 * Class {@code ROIUtil} offers methods to help with the manipulation of
 * regions of interest.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.media.jai.DescriptorUtil">Daniel Léonard</a>
 * @version $Revision$
 */
public class ROIUtil {

//---------------------------
// Constructors
//---------------------------

   /**
    * Allow subclasses only.
    */
   protected ROIUtil() {
   // nothing
      }


//---------------------------
// Class methods
//---------------------------

   /**
    * Creates a rectangular region of interest.
    * @param   x   the X coordinate of the upper-left corner of the rectangle.
    * @param   y   the Y coordinate of the upper-left corner of the rectangle.
    * @param   w   the width of the rectangle.
    * @param   h   the height of the rectangle.
    * @return   the region of interest.
    */
   public static ROI getRectangularROI(double x, double y, double w, double h) {
      Shape rectangle = new Rectangle2D.Double(x, y, w, h);
      ROI roi = new ROIShape(rectangle);
      return roi;
      }

   /**
    * Transform the specified region of interest, assumed to have its origin at
    * <em>(0, 0)</em> into the image space whose origin is
    * <em>(minX, minY)</em>.
    * <p>
    * As an example, assume a rectangular region of interest
    * <em>(x, y, w, h)</em> <em>(10, 10, 10, 10)</em>. If the image
    * <em>(minX, minY)</em> is <em>(10, 10)</em>, the new region of interest
    * will be <em>(20, 20, 10, 10)</em>
    * @param   roi   the region of interest.
    * @param   image    the image.
    * @return    a new region of interest in image space.
    */
   public static ROI toImageSpace(ROI roi, RenderedImage image) {
      int x = image.getMinX();
      int y = image.getMinY();
      AffineTransform transform = AffineTransform.getTranslateInstance(x, y);
      roi = roi.transform(transform);
      return roi;
      }

   }
